package com.icez.base_util_library

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.Target

/**
 *  加载图片 这几个权限必须要有
 * <uses-permission android:name="android.permission.INTERNET"/>
 * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 * <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 * <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 */
class LoadImgUtil {
    companion object {
        val instance: LoadImgUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            LoadImgUtil()
        }
    }

    /**
     *  加载图片
     * 
     * @author Icez
     * @param context 上下文
     * @param imageView 图片控件
     * @param imgUrl 图片地址
     * @param placeholderId 图片没加载之前的图片
     * @param errorId 图片加载失败图片
     * @param fallbackId 图片地址为null的图片
     * @param sizeMultiplier 缩略图大小
     */
    @SuppressLint("ResourceType")
    public fun loadImg(
        context: Context?,
        imageView: ImageView?,
        imgUrl: String?,
        placeholderId:Int = 0,
        errorId:Int = 0,
        fallbackId:Int = 0,
        sizeMultiplier:Float = 0.8f
    ) {
        if (imageView == null) {
            return
        }

        context?.run {
            imageView.post {
                var imgWidth:Int = imageView.measuredWidth
                var imgHeight:Int = imageView.measuredHeight
                Glide.with(this)
                    .load(imgUrl)
                    .thumbnail(Glide.with(this).asDrawable().sizeMultiplier(sizeMultiplier))
                    .override(imgWidth,imgHeight)
                    .placeholder(placeholderId)
                    .error(errorId)
                    .fallback(fallbackId)
                    .centerCrop()
                    .into(imageView)
            }

        }
    }

    /**
     *  获取位图
     * @param activity Activity
     * @param imgUrl 图片地址
     * @param width 生成位图的宽度
     * @param height 生成位图的高度
     */
    public suspend fun getBitmap(activity: AppCompatActivity?,
                         imgUrl:String?,
                         width:Int = Target.SIZE_ORIGINAL,
                         height:Int = Target.SIZE_ORIGINAL): Bitmap?{
        activity?.run {
            return Glide.with(activity)
                .asBitmap()
                .load(imgUrl)
                .submit(width,height)
                .get()
        }
        return null
    }

    /**
     *  获取Drawable
     * @param activity Activity
     * @param imgUrl 图片地址
     * @param width 生成Drawable的宽度
     * @param height 生成Drawable的高度
     */
    public suspend fun getDrawable(activity: AppCompatActivity?,
                         imgUrl:String?,
                         width:Int = Target.SIZE_ORIGINAL,
                         height:Int = Target.SIZE_ORIGINAL): Drawable?{
        activity?.run {
            return Glide.with(activity)
                .asDrawable()
                .load(imgUrl)
                .submit(width,height)
                .get()
        }
        return null
    }


    /**
     *  清除指定控件上面的缓存
     * @param activity Activity
     * @param imageView 图片控件
     */
    public fun clearCacheForView(
        activity: AppCompatActivity?,
        imageView: ImageView?
    ) {
        if (imageView == null) {
            return
        }
        activity?.run {
            Glide.with(this).clear(imageView)
        }
    }

    /**
     *  清楚所有缓存
     * @param context 上下文
     */
    public suspend fun clearAllCache(context: Context?) {
        context?.run {
            Glide.get(this).clearMemory()
            Glide.get(this).clearDiskCache()
        }
    }
}